#include "SList.h"

SLNode* pp(SLNode** pphead, SLDatatype x)
{
	assert(pphead);
	SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
	if (newnode == NULL)
	{
		perror("newnode fail!");
		exit(1);
	}
	else
	{
		newnode->data = x;
		newnode->next = NULL;
	}
	return newnode;
}

void my_printf(SLNode* phead)
{
	assert(phead);
	SLNode* tmp = phead;
	while (tmp)
	{
		printf("%d-> ", tmp->data);
		tmp = tmp->next;
	}
	printf("NULL\n");
}


void SLDpushBack(SLNode** pphead, SLDatatype x)
{
	assert(pphead);
	SLNode*phead= pp(pphead,x);
	if (*pphead == NULL)
	{
		*pphead = phead;
		(*pphead)->data = x;
		(*pphead)->next = NULL;
	}
	else
	{
		SLNode* tmp = *pphead;
		SLNode* ptail = NULL;
		while (tmp)
		{
			ptail = tmp;
			tmp = tmp->next;
		}
		ptail->next = phead;
	}
}

void SLDpushFront(SLNode** pphead, SLDatatype x)
{
	assert(pphead);
	SLNode* phead = pp(pphead, x);
	if (*pphead == NULL)
	{
		*pphead = phead;
	}
	else
	{
		phead->next=*pphead;
		*pphead = phead;
	}
}

void SLDpopBack(SLNode** pphead)
{
	assert(pphead && *pphead);
	SLNode* ptail = NULL;
	SLNode* tmp = *pphead;
	while (tmp->next)
	{
		ptail = tmp;
		tmp = tmp->next;
	}
	free(tmp);
	ptail->next = NULL;
}

void SLDpopFront(SLNode** pphead)
{
	assert(pphead && *pphead);
	SLNode* tmp = *pphead;
	*pphead = tmp->next;
	free(tmp);
}